<template>
  <el-dialog v-if="visible" :visible="visible" width="500px" title="创建剧集" :append-to-body="true" :close-on-click-modal="false" @close="cancel">
    <el-form ref="form" v-loading="loading" :model="form" :rules="formRules" label-width="120px" label-position="left">
      <el-form-item label="剧集名称" prop="name">
        <el-input v-model="form.name" :clearable="true" placeholder="请输入剧集名称" @clear="$refs.form.validateField(['name'])" />
      </el-form-item>
      <el-form-item label="集数" prop="episode_number">
        <el-input v-model="form.episode_number" :clearable="true" placeholder="请输入集数" @clear="$refs.form.validateField(['episode_number'])" />
      </el-form-item>
      <el-form-item label="简介" prop="introduce">
        <el-input ref="introduce" v-model="form.introduce" type="textarea" :rows="8" resize="none" :clearable="true" placeholder="请输入简介" />
        <div style="text-align: right">
          <el-button type="danger" size="mini" @click="form.introduce = ''; $refs.introduce.focus(); $refs.form.validateField(['introduce'])">清空</el-button>
        </div>
      </el-form-item>
      <el-form-item label="剧集图片" prop="icon">
        <upload :file-list="icon" @on-update="iconUpdate" />
      </el-form-item>
      <el-form-item label="剧集视频" prop="video">
        <upload :file-list="video" type="video" @on-update="videoUpdate" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="form.status">
          <el-radio :label="0">显示</el-radio>
          <el-radio :label="1">隐藏</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="cancel">取消</el-button>
      <el-button v-loading="loading" type="primary" @click="enter">确认</el-button>
    </template>
  </el-dialog>
</template>
<script>
import { addEpisode } from '@/api/short'

export default {
  name: 'Create',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    shortId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      loading: false,
      form: {
        short_id: this.shortId,
        name: '',
        episode_number: '',
        introduce: '',
        icon: '',
        video: '',
        status: 0
      },
      icon: [],
      video: [],
      formRules: {
        name: [
          {
            trigger: 'blur', validator(rule, value, callback) {
              if (typeof value !== 'string') {
                return callback(new Error('剧集名称不能为空'))
              }
              if (value.trim().length === 0) {
                return callback(new Error('剧集名称不能为空'))
              }
              return callback()
            }
          }
        ],
        episode_number: [
          {
            trigger: 'blur',
            validator(rule, value, callback) {
              if (value.trim().length === 0) {
                return callback(new Error('请输入数值'))
              }
              if (!/^\d+$/.test(value)) {
                return callback(new Error('请输入数值'))
              }
              return callback()
            }
          }
        ],
        introduce: [
          {
            trigger: 'blur',
            validator(rule, value, callback) {
              if (typeof value !== 'string') {
                return callback(new Error('简介不能为空'))
              }
              if (value.trim().length === 0) {
                return callback(new Error('简介不能为空'))
              }
              return callback()
            }
          }
        ],
        icon: [
          {
            trigger: 'blur',
            validator(rule, value, callback) {
              if ((typeof value) !== 'string') {
                return callback(new Error('剧集图片不能为空'))
              }
              if (value.trim().length === 0) {
                return callback(new Error('剧集图片不能为空'))
              }
              return callback()
            }
          }
        ],
        video: [
          {
            trigger: 'blur',
            validator(rule, value, callback) {
              if ((typeof value) !== 'string') {
                return callback(new Error('剧集视频不能为空'))
              }
              if (value.trim().length === 0) {
                return callback(new Error('剧集视频不能为空'))
              }
              return callback()
            }
          }
        ]
      }
    }
  },
  methods: {
    async enter() {
      try {
        await this.$refs.form.validate()
        try {
          this.loading = true
          await addEpisode(this.form)
          this.loading = false
          this.$emit('enter')
        } catch (error) {
          this.$errorMessage()
          this.loading = false
        }
      } catch (error) {
        this.$errorMessage('表单填写有误')
      }
    },
    videoUpdate(fileList) {
      this.video = fileList
      if (this.video.length > 0) {
        this.form.video = this.video[0].url
      } else {
        this.form.video = ''
      }
      this.$refs.form.validateField(['video'])
    },
    iconUpdate(fileList) {
      this.icon = fileList
      if (this.icon.length > 0) {
        this.form.icon = this.icon[0].url
      } else {
        this.form.icon = ''
      }
      this.$refs.form.validateField(['icon'])
    },
    cancel() {
      this.$emit('cancel')
    }
  }
}
</script>
